$(function () {
  var playtimeout = null, displayindex = 0, playing = true;
  // var token = 'c601d0c6-96df-4d79-8c98-ab7490af9d06';

  var BASEDOMAIN = 'http://61.160.96.90:8858'
  $.ajaxSetup({ //发送请求前触发
    beforeSend: function (xhr, argument) {
      if (argument.url.indexOf('.json') > -1 || argument.url.indexOf('weather') > -1) {

      } else {
        if (argument.url.indexOf('apis') > -1) {

        } else {
          argument.url = '/apis/bs/sqlInfo/getData?id=' + argument.url
        }
        argument.url = BASEDOMAIN + argument.url
      }
    }
  })
  // 接口映射
  var apis = {
    chart2: '/apis/bsNotice/getBsNotice',
    chart4: 145,
    chart6: 146,
    play: [{  // 人社
      map: {
        url: './static/js/map/320000_full.json',
        id: [147],
        data: []
      },
      chart9: '人社实时交付状态',
      chart24: '人社SLA预警',
      chart12: '紧急告警占比',
      chart22: '当月同比增幅',
      chart10: '告警总量(月)',
      chart14: '工单总量(月)',
      chart16: '处理中占比',
      chart17: '未结单占比',
      chart20: '告警总量(年)',
      chart13: 148,
      chart23: 149,
      chart11: 150,
      chart21: 151,
      chart15: 152,
      chart18: 179,
      chart19: 178,
      chart27: 180,
      chart25: 181,
      chart26: 182,
      chart29: '人社'
    }, {     // 商业连锁
      map: {
        url: './static/js/map/china.json',
        data: [],
        id: [153, 154]
      },
      chart9: '商业连锁实时交付状态',
      chart24: '德克士门店报修预警',
      chart12: '环比(天)增幅',
      chart22: '转呈环比增长(日)',
      chart10: '德克士总订单(天)',
      chart14: '工单总量(天)',
      chart16: 'DK完成率',
      chart17: 'BK转呈',
      chart20: 'BK转呈总数(月)',
      chart13: 156,
      chart23: 161,
      chart11: 155,
      chart21: 160,
      chart15: 157,
      chart18: 158,
      chart19: 159,
      chart27: 184,
      chart25: 162,
      chart26: 186,
      chart29: '商业连锁'
    }, {   // 技防
      map: {
        url: './static/js/map/320200_full.json',
        data: [],
        id: [163, 183]
      },
      chart9: '技防实时交付状态',
      chart24: '技防SLA预警',
      chart12: '环比(月)增幅',
      chart22: '总完成率',
      chart10: '总工单数(月)',
      chart14: '未完成工单数(月)',
      chart16: '处理中占比',
      chart17: '未接单占比',
      chart20: '总工单数(年)',
      chart13: 165,
      chart23: 175,
      chart11: 164,
      chart21: 167,
      chart15: 166,
      chart18: 176,
      chart19: 177,
      chart27: 187,
      chart25: 168,
      chart26: 185,
      chart29: '技防'
    }
    ]
  }

  // 大屏左侧
  $.post(apis.chart2, { id: 8 }, function (data) {
    $('#chart1').html(data.data.name)
    var content = data.data.content
    console.log(content)
    content = content.replace(/\r\n/g,"<br>")
    $('#chart2').html(content)
  }, 'json')
  $.post(apis.chart4, function (data) {

    var tags = data.data

    app.$data.tags = tags
    app.init()
  }, 'json')
  $.post(apis.chart6, function (data) {
    renderChart6(data.data)
  }, 'json')

  var fixedNumber = function (val){
    // var valstring = val.toString()
    // if(valstring.indexOf('.') > -1 && valstring.length > 6){
    //   return val.toFixed(2)
    // } else {
    //   return val
    // }
    return Math.round(val)
  }

  // 渲染
  var renderStart = function () {
    var curlapis = apis.play[displayindex]
    // 文案
    $('#chart9').html(curlapis.chart9)
    $('#chart24').html(curlapis.chart24)
    $('#chart12').html(curlapis.chart12)
    $('#chart22').html(curlapis.chart22)
    $('#chart10').html(curlapis.chart10)
    $('#chart14').html(curlapis.chart14)
    $('#chart16').html(curlapis.chart16)
    $('#chart17').html(curlapis.chart17)
    $('#chart20').html(curlapis.chart20)
    $('#chart29').html(curlapis.chart29)
    // 地图
    curlapis.map.data = []
    var rsindex = 0
    for (var i = 0; i < curlapis.map.id.length; i++) {
      var id = curlapis.map.id[i]
      $.ajax({  
        type : "post",  
        url : id,
        async : false,
        success : function(data){  
          var mapPoints = []
          // if(data.data[i].Name != '江苏开拓'){
          if(displayindex == 0){
            mapPoints = data.data
          }
          if (displayindex == 1 || displayindex == 2) { // 第2，3 屏幕的数据需要区分类别
            for (var i = 0; i < data.data.length; i++) {
              if (rsindex == 0) {
                data.data[i].type = 'A'
              } else {
                data.data[i].type = 'B'
              }
              mapPoints.push(data.data[i])
            }
          }
          rsindex++
          curlapis.map.data = curlapis.map.data.concat(mapPoints)
          if (rsindex == curlapis.map.id.length) {
            renderMap(curlapis.map, displayindex)
          } 
        }
      });
    }
    // 地图右侧统计信息
    $.post(curlapis.chart27, function (data) {
      var statistics = data.data
      var html = ''
      for (var i = 0; i < statistics.length; i++) {
        var item = statistics[i]
        html += '<dt>' + item.name + '</dt>' + '<dd>' + item.value + '</dd>'
      }
      var title = ''
      switch (displayindex) {
        case 0: title = '当月告警统计'; break;
        case 1: title = '月工单统计'; break;
        case 2: title = '当月报修统计'; break;
      }
      $('#chart27 > h3').html(title)
      $('#chart27 > #statistics').html(html)
    })



    // 右上角
    $.post(curlapis.chart11, function (data) {
      var val = +data.data[0].value
      $('#chart11').html(val)
    }, 'json')
    $.post(curlapis.chart13, function (data) {
      var val = fixedNumber(+data.data[0].value * 100);
      if (displayindex == 0) {
        renderChart13({
          value: val,
          color: '#6eafe8'
        })
      } else {
        if (+val > 0) {
          renderChart13({
            value: val,
            color: '#6eafe8',
            trend: 1
          })
        } else {
          renderChart13({
            value: val,
            color: '#6eafe8',
            trend: 2
          })
        }
      }
    }, 'json')
    $.post(curlapis.chart15, function (data) {
      var val = +data.data[0].value
      $('#chart15').html(val)
    })
    $.post(curlapis.chart18, function (data) {
      var val = fixedNumber(+data.data[0].value * 100);
      // if (displayindex == 0) {
        renderChart18({
          value: val,
          color: '#6ee8bf'
        })
      // } else {
      //   if (+val > 0) {
      //     renderChart18({
      //       value: val,
      //       color: '#6ee8bf',
      //       trend: 1
      //     })
      //   } else {
      //     renderChart18({
      //       value: val,
      //       color: '#6ee8bf',
      //       trend: 2
      //     })
      //   }
      // }
    })
    $.post(curlapis.chart19, function (data) {
      var val = fixedNumber(+data.data[0].value * 100);
      // if (displayindex == 0) {
        renderChart19({
          value: val,
          color: '#e8cf6e'
        })
      // } else {
      //   if (+val > 0) {
      //     renderChart19({
      //       value: val,
      //       color: '#e8cf6e',
      //       trend: 1
      //     })
      //   } else {
      //     renderChart19({
      //       value: val,
      //       color: '#e8cf6e',
      //       trend: 2
      //     })
      //   }
      // }

    })
    $.post(curlapis.chart21, function (data) {
      var val = (+data.data[0].value)
      $('#chart21').html(val);
    }, 'json')
    $.post(curlapis.chart23, function (data) {
      var val = fixedNumber(+data.data[0].value * 100);
      // var val = ((+data.data[0].value) * 100).toFixed(2)
      if (displayindex == 0) {
        if (+val > 0) {
          renderChart23({
            value: val,
            color: '#e86e6e',
            trend: 1
          })
        } else {
          renderChart23({
            value: val,
            color: '#e86e6e',
            trend: 2
          })
        }
      } else {
        renderChart23({
          value: val,
          color: '#e86e6e'
        })
      }

    }, 'json')


    //右下角
    $.post(curlapis.chart25, function (data) {
      var title = ''
      switch (displayindex) {
        case 0: title = '告警占比'; break;
        case 1: title = '分类占比'; break;
        case 2: title = '故障类别'; break;
      }
      var chartData = {
        title: title,
        names: [],
        values: data.data
      }
      for (var i = 0; i < data.data.length; i++) {
        chartData.names.push(data.data[i].name)
      }
      renderChart25(chartData)
    }, 'json')

    app.$data.tableColumn = []
    app.$data.tableDate = []
    app.$nextTick(function() {
      $.getJSON('/apis/bs/field-mapping?sqlId=' + curlapis.chart26, function (columnrs) {
        try {
          var fieldMapping = eval("(" + columnrs.data.fieldMapping + ")")[0]
          var columns = []
          for (key in fieldMapping) {
            columns.push({
              prop: key,
              label: fieldMapping[key],
            })
          }
          columns[0].width = '200px'
          app.$data.tableColumn = columns
          $.post(curlapis.chart26, function (data) {
            let tableDate = []
            for (var i = 0; i < 7; i++) {
              tableDate.push(data.data[i])
            }
            app.$data.tableDate = tableDate
            app.$nextTick(function() {
              app.initRoll();
            });
          }, 'json')
        } catch (error) {
          alert('列头返回格式不是JSON')
        }

      })
    })
  }

  // 控制屏幕切换
  var play = function () {
    renderStart()
    playtimeout = setInterval(function () {
      if (displayindex >= 2) {
        window.location.reload()
      } else {
        displayindex = displayindex + 1
      }
      renderStart()
    }, 2 * 60 * 1000)
  }
  play()

  // 天气
  $.getJSON("/apis/bs/getWeather", { cityCode: 101190201 }, function (data, textStatus, jqXHR) {
    var todayWeather = data.data.data.forecast[0]
    var low = todayWeather.low.replace('低温 ', '')
    var high = todayWeather.high.replace('高温 ', '')
    $('#weather').html(todayWeather.type + '&nbsp;&nbsp;' + low + '~' + high + '&nbsp;&nbsp;' + todayWeather.fx + todayWeather.fl)
  })

  // 时间
  moment.locale('zh-cn');
  $('#nowtime').html(moment().format('YYYY年MM月DD日 dddd HH:mm:ss'));
  setInterval(function () {
    $('#nowtime').html(moment().format('YYYY年MM月DD日 dddd HH:mm:ss'));
  }, 1000);


  $('#console .stop').click(function () {
    $('#console .stop').hide()
    $('#console .start').show()
    clearInterval(playtimeout)
  })
  $('#console .start').click(function () {
    $('#console .start').hide()
    $('#console .stop').show()
    play()
  })
})